<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button type="primary" icon="el-icon-plus" size="small" @click="add">新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table
              :data="list"
              style="width: 100%"
            >
              <el-table-column
                type="index"
                :index="indexMethod"
                label="序号"
                width="180"
              />
              <el-table-column
                prop="name"
                label="角色名称"
                width="300"
              />
              <el-table-column
                prop="description"
                label="描述"
                width="400"
              />
              <el-table-column
                label="操作"
              >
                <template slot-scope="scope">
                  <el-button type="success" size="small" @click="assign(scope.row.id)">分配权限</el-button>
                  <el-button type="primary" size="small" @click="edit(scope.row)">编辑</el-button>
                  <el-button type="danger" size="small" @click="del(scope.row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-tab-pane>
          <!-- 分页 -->
          <div class="block" style="text-align: center; margin-top: 15px;">
            <el-pagination
              background
              :current-page="pageParams.page"
              :page-sizes="[1, 2, 3, 5]"
              :page-size="pageParams.pagesize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </div>
        </el-tabs>
      </el-card>

      <!-- 添加编辑弹出框 -->
      <el-dialog
        :title="isEdit?'编辑':'添加'"
        :visible.sync="dialogFormVisible"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        @close="close"
      >
        <el-form ref="form" :model="form" label-width="100px" :rules="rules">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="form.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="form.description" />
          </el-form-item>
        </el-form>
        <el-row slot="footer" class="dialog-footer">
          <el-button size="small" @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" size="small" @click="submit">确 定</el-button>
        </el-row>
      </el-dialog>

      <!-- 分配权限的弹层 -->
      <el-dialog
        title="分配权限(一级为路由页面查看权限-二级为按钮操作权限)"
        :visible.sync="showDialogAssign"
      >
        <assign-permission v-if="showDialogAssign" :user-id="userId" @close="showDialogAssign=false" />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getList, delPeople, addPeople, editPeople } from '@/api/settings'
// 引入分配权限子组件
import assignPermission from './assignPermission'
export default {
  name: '',
  components: { assignPermission },
  props: {},
  data() {
    return {
      userId: '', // 当前角色的id
      showDialogAssign: false, // 分配权限显示隐藏
      list: [],
      pageParams: {
        page: 1,
        pagesize: 2
      },
      total: 0,
      form: {
        name: '',
        description: ''
      },
      dialogFormVisible: false, // 添加编辑显示隐藏
      isEdit: false,
      rules: {
        name: [
          { required: 'true', message: '不能为空', trigger: 'blur' }
        ],
        description: [
          { required: 'true', message: '不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {},
  watch: {},
  created() {
    this.doGetList()
  },
  mounted() {},
  methods: {
    // 表格序号排序
    indexMethod(index) {
      return (this.pageParams.page - 1) * this.pageParams.pagesize + index + 1
    },
    // 每页条数改变时
    handleSizeChange(val) {
      this.pageParams.pagesize = val
      this.doGetList()
    },
    // 页码改变时
    handleCurrentChange(val) {
      this.pageParams.page = val
      this.doGetList()
    },
    // 获取数据
    async doGetList() {
      try {
        const res = await getList(this.pageParams)
        this.total = res.data.total
        this.list = res.data.rows
      } catch (err) {
        console.log(err)
        this.$message.error('获取数据失败')
      }
    },
    //  删除数据
    async isDel(id) {
      try {
        await delPeople(id)
        this.$message({ type: 'success', message: '删除成功!' })
        // 判断当前的页码
        if (this.list.length === 1) {
          this.pageParams.page--
          if (this.pageParams.page <= 0) {
            this.pageParams.page = 1
          }
        }
        // 重新渲染
        this.doGetList()
      } catch (err) {
        console.log(err)
        this.$message.error('删除失败')
      }
    },
    // 点击删除按钮
    del(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.isDel(id)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 添加角色
    async isAdd() {
      try {
        await addPeople(this.form)
        this.$message.success('添加成功')
        // 关闭弹框
        this.dialogFormVisible = false
        // 判断页码,跳转到当前添加数据额的页码
        this.pageParams.page = Math.ceil(++this.total / this.pageParams.pagesize)
        // 重新获取数据
        this.doGetList()
      } catch (err) {
        console.log(err)
        this.$message.error('添加失败')
      }
    },
    // 编辑更改角色
    async isEditUser() {
      try {
        await editPeople(this.form)
        this.$message.success('编辑成功')
        // 关闭弹框
        this.dialogFormVisible = false
        // 重新渲染
        this.doGetList()
      } catch (err) {
        console.log(err)
        this.$message.error('编辑失败')
      }
    },
    // 点击确定按钮
    submit() {
      this.$refs.form.validate(valide => {
        if (!valide) return
        this.isEdit ? this.isEditUser() : this.isAdd()
      })
    },
    // 点击编辑按钮
    edit(row) {
      this.isEdit = true
      this.form = { ...row }
      this.dialogFormVisible = true
    },
    // 点击新增按钮
    add() {
      this.isEdit = false
      this.dialogFormVisible = true
    },
    // 弹框关闭时 都会触发这个函数
    close() {
      this.form = {
        name: '',
        description: '',
        id: ''
      }
      this.$refs.form.resetFields()
    },
    // 点击分配权限
    assign(id) {
      this.userId = id
      this.showDialogAssign = true
    }
  }
}
</script>

<style scoped lang='less'>

</style>
